package com.wan37.logic.handler.message;

import com.wan37.logic.component.Worker;
import com.wan37.logic.domain.vo.MessageVO;
import com.wan37.logic.handler.LogicServerHandler;
import com.wan37.logic.handler.MsgHandler;
import com.wan37.protobuf.analysis.ParseMap;
import com.wan37.protobuf.message.client2server.game.Game;
import com.wan37.protobuf.message.internal.Internal;
import com.wan37.protobuf.util.ServerProtoUtils;
import io.netty.buffer.ByteBuf;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 客户端发送的游戏消息处理
 *
 * @author : luoyong
 * @date : 2020-05-28 10:12
 **/
public class CGameHandler extends MsgHandler {
    private static final Logger logger = LoggerFactory.getLogger(CGameHandler.class);

    /**
     * MsgHandler构造函数
     *
     * @param messageVO
     */
    public CGameHandler(MessageVO messageVO) {
        super(messageVO);
    }

    @Override
    public void execute(Worker worker) throws Exception {
        Game.CGameMsg cGameMsg = (Game.CGameMsg) messageVO.getMessage();
        logger.info("LogicServer收到了处理请求: {}", cGameMsg);


        //包装成GTransfer转发给GateServer
        ByteBuf sendBuf = ServerProtoUtils.pack2Server(Internal.DestType.Client,
                messageVO.getNetId(), messageVO.getUserId(), ParseMap.getPtoNum(cGameMsg), cGameMsg);
        //发送给GateServer
        LogicServerHandler.getGateConnCtx().writeAndFlush(sendBuf);
        logger.info("[LogicServer] send [{}] message to [GateServer]", cGameMsg.getClass().getSimpleName());
    }
}
